<?php
/**
 * 数据列表生成器
 */

namespace Todo\Builder;


use Think\Controller;

class ListBuilder extends Controller
{
    /**
     * 列表属性
     * @var array
     */
    protected $_attr = array(
        'action' => '', //搜索的URL
        'batch'  => true, //是否允许批量操作
        'title'  => ''
    );


    /**
     * 列表的数据
     * @var array
     */
    protected $_data = array();

    /**
     * 分页
     * @var string
     */
    protected $_pages = '';

    protected $_data_key = 'id';

    /**
     * 数据表的列
     * @var array
     */
    protected $_columns = array();

    /**
     * 搜索字段
     * @var array
     */
    protected $_searchs = array();

    /**
     * 按钮，一般在列表上边或下边
     * @var array
     */
    protected $_buttons = array();

    /**
     * 操作，一般在单行列表数据的左边或右边
     * @var array
     */
    protected $_operations = array();

    protected $_style = '';

    public function setStyle($style=''){
        $this->_style = $style;
    }

    /**
     * 设置属性
     * @param $atttName
     * @param string $attrVal
     */
    public function setAttr($atttName, $attrVal=''){
        $this->_attr[$atttName] = $attrVal;
    }

    /**
     * 加入一个数据列
     * @param $name 数据名
     * @param $title 列标题
     * @param $type=text 字段类型:date,dict,datetime,text,number,relation(关联表)
     * @param $search=false 是否支持搜索
     * @param $option 额外参数
     * @return $this
     */
    public function addColumn($name, $title, $type ='text', $search=false, $option=null) {
        $column = array(
            'name'  => $name,
            'title' => $title,
            'search'  => $search,
            'type'  => $type,
            'option' => $option,
        );
        if($name == $this->_data_key) array_unshift($this->_columns, $column);
        else $this->_columns[] = $column;

        if($search) $this->_searchs[] = $search;

        return $this;
    }

    /**
     * 添加页面上按钮，一般在列表上方或下方
     * @param $type 默认支持:add,delete,disable,enable,restore,verify
     * @param null $attr
     * @param string $url=''
     */
    public function addButton($type, $attr=null, $url=''){
        switch($type){
            case 'add':
                $this->_buttons[] = array(
                    'type' => 'link',
                    'title' => '添加',
                    'class' => 'btn btn-primary',
                    'url'  => $url ? $url : U(MODULE_NAME.'/'.CONTROLLER_NAME.'/add'),
                    'attr'  => $attr
                );
                break;
            case 'delete':
                $this->_buttons[] = array(
                    'type' => 'action',
                    'title' => '批量删除',
                    'class' => 'btn btn-danger',
                    'form' => '#listForm',
                    'url'  => $url ? $url : U(MODULE_NAME.'/'.CONTROLLER_NAME.'/delete'),
                    'attr'  => $attr
                );
                break;
            case 'disable':
                $this->_buttons[] = array(
                    'type' => 'action',
                    'title' => '禁用',
                    'class' => 'btn btn-warning',
                    'form' => '#listForm',
                    'url'  => $url ? $url : U(MODULE_NAME.'/'.CONTROLLER_NAME.'/disable'),
                    'attr'  => $attr
                );
                break;
            case 'enable':
                $this->_buttons[] = array(
                    'type' => 'action',
                    'title' => '启用',
                    'class' => 'btn btn-success',
                    'form' => '#listForm',
                    'url'  => $url ? $url : U(MODULE_NAME.'/'.CONTROLLER_NAME.'/enable'),
                    'attr'  => $attr
                );
                break;
            case 'restore':
                $this->_buttons[] = array(
                    'type' => 'action',
                    'title' => '还原',
                    'class' => 'btn btn-info',
                    'form' => '#listForm',
                    'url'  => $url ? $url : U(MODULE_NAME.'/'.CONTROLLER_NAME.'/restore'),
                    'attr'  => $attr
                );
                break;
            case 'verify':
                $this->_buttons[] = array(
                    'type' => 'action',
                    'title' => '审核通过',
                    'class' => 'btn btn-info',
                    'form' => '#listForm',
                    'url'  => $url ? $url : U(MODULE_NAME.'/'.CONTROLLER_NAME.'/verify'),
                    'attr'  => $attr
                );
                break;
            case 'import':
                $this->_buttons[] = array(
                    'type' => 'link',
                    'title' => '<span class="glyphicon glyphicon-import">导入</span>',
                    'class' => 'btn btn-default',
                    'form' => '#listForm',
                    'url'  => $url ? $url : U(MODULE_NAME.'/'.CONTROLLER_NAME.'/import'),
                    'attr'  => $attr
                );
                break;
            case 'export':
                $this->_buttons[] = array(
                    'type' => 'link',
                    'title' => '<span class="glyphicon glyphicon-export">导出</span>',
                    'class' => 'btn btn-default',
                    'form' => '#listForm',
                    'url'  => $url ? $url : U(MODULE_NAME.'/'.CONTROLLER_NAME.'/export'),
                    'attr'  => $attr
                );
                break;
            default:
                $this->_buttons[] = $attr;
                break;
        }
    }
    /**
     * 添加数据操作，一般在数据行的左边或右边
     * @param $type 默认支持:edit,delete,disable,enable,restore,verify
     * @param null $attr
     * @param string $url=''
     */
    public function addOperation($type, $attr=null, $url=''){
        switch($type){
            case 'edit':
                $this->_operations[] = array(
                    'title' => '编辑',
                    'class' => 'label label-primary',
                    'href'  => $url ? $url : U(MODULE_NAME.'/'.CONTROLLER_NAME.'/edit'),
                    'attr'  => $attr
                );
                break;
            case 'delete':
                $this->_operations[] = array(
                    'title' => '删除',
                    'class' => 'label label-danger',
                    'href'  => $url ? $url : U(MODULE_NAME.'/'.CONTROLLER_NAME.'/delete'),
                    'attr'  => $attr
                );
                break;
            case 'disable':
                $this->_operations[] = array(
                    'title' => '禁用',
                    'class' => 'label label-warning',
                    'href'  => $url ? $url : U(MODULE_NAME.'/'.CONTROLLER_NAME.'/disable'),
                    'attr'  => $attr
                );
                break;
            case 'enable':
                $this->_operations[] = array(
                    'title' => '启用',
                    'class' => 'label label-success',
                    'href'  => $url ? $url : U(MODULE_NAME.'/'.CONTROLLER_NAME.'/enable'),
                    'attr'  => $attr
                );
                break;
            case 'restore':
                $this->_operations[] = array(
                    'title' => '还原',
                    'class' => 'label label-info',
                    'href'  => $url ? $url : U(MODULE_NAME.'/'.CONTROLLER_NAME.'/restore'),
                    'attr'  => $attr
                );
                break;
            case 'view':
                $this->_operations[] = array(
                    'title' => '查看',
                    'class' => 'label label-info',
                    'href'  => $url ? $url : U(MODULE_NAME.'/'.CONTROLLER_NAME.'/view'),
                    'attr'  => $attr
                );
                break;
            case 'verify':
                $this->_operations[] = array(
                    'title' => '审核',
                    'class' => 'label label-info',
                    'href'  => $url ? $url : U(MODULE_NAME.'/'.CONTROLLER_NAME.'/view'),
                    'attr'  => $attr
                );
                break;
            default:
                $this->_operations[] = $attr;
                break;
        }
    }

    /**
     * 列表数据
     * @param $data
     */
    public function setData($data){
        $this->_data = $data;
    }

    /**
     * 分页
     * @param $pages
     */
    public function setPages($pages){
        $this->_pages = $pages;
    }


    /**
     * 列表数据主键
     * @param $dataKey
     */
    public function setDataKey($dataKey){
        $this->_data_key = $dataKey;
    }


    /**
     * 渲染模板前赋值
     */
    public function display($tplFile = ''){
        $this->assign('attr', $this->_attr);
        $this->assign('columns', $this->_columns);
        $this->assign('searchs', $this->_searchs);
        $this->assign('operations', $this->_operations);
        $this->assign('buttons', $this->_buttons);
        $this->assign('list', $this->_data);
        $this->assign('pages', $this->_pages);
        $this->assign('dataKey', $this->_data_key);

        parent::display($tplFile);
    }

    /**
     * 生成列表页面
     * @param string $viewFile 页面保存路径
     */
    public function builder($viewFile){
        $this->assign('attr', $this->_attr);
        $this->assign('columns', $this->_columns);
        $this->assign('searchs', $this->_searchs);
        $this->assign('operations', $this->_operations);
        $this->assign('buttons', $this->_buttons);
        $this->assign('list', $this->_data);
        $this->assign('pages', $this->_pages);
        $this->assign('dataKey', $this->_data_key);
        
        $tplHtml = file_get_contents(APP_PATH. '../data/tpl/adminlte/'.($this->_style ? $this->_style : 'admin').'_list.html');
        $listHtml =  $this->fetch(APP_PATH .'../data/tpl/builder/list_builder.html');
        file_put_contents($viewFile, str_replace('____CONTENT____', $listHtml, $tplHtml));
    }

    /**
     * 生成审核页面
     * @param string $viewFile 页面保存路径
     */
    public function verify($viewFile){
        $this->assign('attr', $this->_attr);
        $this->assign('columns', $this->_columns);
        $this->assign('searchs', $this->_searchs);
        $this->assign('operations', $this->_operations);
        $this->assign('buttons', $this->_buttons);
        $this->assign('list', $this->_data);
        $this->assign('pages', $this->_pages);
        $this->assign('dataKey', $this->_data_key);

        $tplHtml = file_get_contents(APP_PATH. '../data/tpl/adminlte/'.($this->_style ? $this->_style : 'admin').'_verify.html');
        $verifyHtml =  $this->fetch(APP_PATH .'../data/tpl/builder/verify_builder.html');
        file_put_contents($viewFile, str_replace('____CONTENT____', $verifyHtml, $tplHtml));
    }
}